(function($) {
	var myScroll, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset;
	var userDataSum, addNum = 0,
		stepLength = 5,
		sumL = 0;

	function refreshScrollBar() {
		console.log("刷新滚动条");
		setTimeout(function() {
			myScroll.refresh();
		}, 1000);
	}

	function pullDownAction() {
		console.log("下拉");
		setTimeout(function() {
			(function reloadPage() {
				if(GetQueryString("_rf")) {
					window.location.href = window.location.href.replace(GetQueryString("_rf"), Math.random());
				} else {
					window.location.href = window.location.href + (window.location.href.indexOf("?") > -1 ? "&" : "?") + "_rf=" + Math.random();
				}
			})();
			myScroll.refresh();
			console.log($("#thelist").height())
		}, 1000);
	}

	function pullUpAction() {
		console.log("上拉");
		setTimeout(function() {
			add(userDataSum);
			console.log($("#thelist").height())
			myScroll.refresh();
		}, 1000);
	}

	function loaded() {
		pullDownEl = document.getElementById('pullDown');
		pullDownOffset = pullDownEl.offsetHeight;
		pullUpEl = document.getElementById('pullUp');
		pullUpOffset = pullUpEl.offsetHeight;
		myScroll = new iScroll('wrapper', {
			useTransition: true,
			topOffset: pullDownOffset,
			onRefresh: function() {
				if(pullDownEl.className.match('loading')) {
					pullDownEl.className = '';
					pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
				} else if(pullUpEl.className.match('loading')) {
					pullUpEl.className = '';
					pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
				}
			},
			onScrollMove: function() {
				if(this.y > 5 && !pullDownEl.className.match('flip')) {
					pullDownEl.className = 'flip';
					pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Release to refresh...';
					this.minScrollY = 0;
				} else if(this.y < 5 && pullDownEl.className.match('flip')) {
					pullDownEl.className = '';
					pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
					this.minScrollY = -pullDownOffset;
				} else if(this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
					pullUpEl.className = 'flip';
					pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...';
					this.maxScrollY = this.maxScrollY;
				} else if(this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
					pullUpEl.className = '';
					pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
					this.maxScrollY = pullUpOffset;
				}
			},
			onScrollEnd: function() {
				if(pullDownEl.className.match('flip')) {
					pullDownEl.className = 'loading';
					pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Loading...';
					refreshScrollBar();
					pullDownAction();
				} else if(pullUpEl.className.match('flip')) {
					pullUpEl.className = 'loading';
					pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Loading...';
					if(addNum < sumL) {
						pullUpAction();
					} else {
						pullUpEl.querySelector('.pullUpLabel').innerHTML = '数据已全部加载完毕';
						pullUpEl.querySelector('.pullUpLabel').innerHTML = "数据已全部加载完毕！";
					}
				}
			}
		});
		setTimeout(function() {
			document.getElementById('wrapper').style.left = '0';
		}, 800);
	}
	document.addEventListener('touchmove', function(e) {
		e.preventDefault();
	}, false);
	document.addEventListener('DOMContentLoaded', function() {
		setTimeout(loaded, 200);
	}, false);

	function GetQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		return(r != null && r.length >= 2) ? decodeURI(r[2]) : null;
	}
	user();

	function user() {
		$.ajax({
			type: "get",
			url: "./js/json.json",
			dataType: "json",
			success: function(data) {
				userDataSum = data;
				console.log(userDataSum.body.totalNum);
				add(userDataSum)
			}
		})
	}

	function add(data) {
		sumL = data.body.totalNum;
		if((sumL - addNum) < stepLength) {
			stepLength = sumL % stepLength;
		}
		addNum += stepLength;
		if(addNum >= sumL) {
			addNum = sumL;
		}
		var el, litext = "",
			i;
		el = el = $('#thelist');
		for(i = addNum - stepLength + 1; i < addNum + 1; i++) {
			litext += '' + '<li>aaa' + i + '</li>'
		}
		el.append(litext);
		refreshScrollBar();
	}
})(jQuery);